package com.jams.rbac.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jams.common.core.system.JamsResponse;
import com.jams.rbac.api.entity.User;
import com.jams.rbac.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 用户表
 *
 * @author linxb
 * @date 2019-12-14 14:27:30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Api(value = "user", tags = "用户表管理")
public class UserController {

    private final  UserService userService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param user 用户表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public JamsResponse getUserPage(Page page, User user) {
        return JamsResponse.ok(userService.page(page, Wrappers.query(user)));
    }

    /**
     * 列表查询
     * @param user 用户表
     * @return
     */
    @ApiOperation(value = "列表查询", notes = "列表查询")
    @GetMapping("/list")
    public JamsResponse getUserList(User user) {
        return JamsResponse.ok(userService.list(Wrappers.query(user)));
    }

    /**
     * 通过id查询用户表
     * @param userId id
     * @return JamsResponse
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{userId}")
    public JamsResponse getById(@PathVariable("userId") Integer userId) {
        return JamsResponse.ok(userService.getById(userId));
    }

    /**
     * 新增用户表
     * @param user 用户表
     * @return JamsResponse
     */
    @ApiOperation(value = "新增用户表", notes = "新增用户表")
    @PostMapping
    public JamsResponse save(@RequestBody User user) {
        return JamsResponse.ok(userService.save(user));
    }

    /**
     * 修改用户表
     * @param user 用户表
     * @return JamsResponse
     */
    @ApiOperation(value = "修改用户表", notes = "修改用户表")
    @PutMapping
    public JamsResponse updateById(@RequestBody User user) {
        return JamsResponse.ok(userService.updateById(user));
    }

    /**
     * 通过id删除用户表
     * @param userId id
     * @return JamsResponse
     */
    @ApiOperation(value = "通过id删除用户表", notes = "通过id删除用户表")
    @DeleteMapping("/{userId}")
    public JamsResponse removeById(@PathVariable Integer userId) {
        return JamsResponse.ok(userService.removeById(userId));
    }

    /********/

    /**
     * 获取指定用户全部信息
     *
     * @return 用户信息
     */
    //@Inner
    @GetMapping("/info/{username}")
    public JamsResponse info(@PathVariable String username) {
        User user = userService.getOne(Wrappers.<User>query()
                .lambda().eq(User::getUsername, username));
        if (user == null) {
            return JamsResponse.failed(null, String.format("用户信息为空 %s", username));
        }
        return JamsResponse.ok(userService.findUserInfo(user));
    }

}
